/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import model.*;

/**
 *
 * @author HELLO
 */
@Named(value = "u_editProfileManagedBean")
@SessionScoped
public class EditProfileManagedBean implements Serializable {

    @EJB
    private LikingsFacade likingsFacade;
    @EJB
    private CountryFacade countryFacade;
    @EJB
    private StateFacade stateFacade;
    @EJB
    private Member1Facade member1Facade;
    private List<State> selectStateList = new ArrayList<>();
    private Country editedCountry = new Country();
    private List<String> editedLikings = new ArrayList<>();

    public List<State> getSelectStateList() {
        return selectStateList;
    }

    public void setSelectStateList(List<State> selectStateList) {
        this.selectStateList = selectStateList;
    }

    public Country getEditedCountry() {
        return editedCountry;
    }

    public void setEditedCountry(Country editedCountry) {
        this.editedCountry = editedCountry;
    }

    public List<String> getEditedLikings() {
        return editedLikings;
    }

    public void setEditedLikings(List<String> editedLikings) {
        this.editedLikings = editedLikings;
    }

    public EditProfileManagedBean() {
    }

    public void edit() {
        MemberManagedBean mmb = get_MemberManagedBean();
        Member1 editedMember = mmb.getMember();
        editedMember.setIDMarital(new Marital(editedMember.getIDMarital().getId()));
        editedMember.setIDState(new State(editedMember.getIDState().getId()));
        editedMember.setIDLanguage(new Language(editedMember.getIDLanguage().getId()));
        editedMember.setIDReligion(new Religion(editedMember.getIDReligion().getId()));
        editedMember.setIDCasle(new Caste(editedMember.getIDCasle().getId()));
        editedMember.setIDPermission(new Role(editedMember.getIDPermission().getId()));
        List<Likings> ListLiking = new ArrayList<>();
        for (String s : editedLikings) {
            Integer i = Integer.parseInt(s);
            ListLiking.add(likingsFacade.find(i));
        }
        editedMember.setLikingsList(ListLiking);
        member1Facade.edit_Liking(ListLiking, editedMember.getId());
        this.member1Facade.edit(editedMember);

        mmb.RefreshMember();
        //mmb.setMember(member1Facade.find(editedMember.getId()));
    }

    public void handleStateChange() {
        List<State> Ls = new ArrayList<>();
        //editedMember.getIDState().getIDCountry().getId()
        for (State s : stateFacade.getStateByCountryId(editedCountry.getId())) {
            Ls.add(s);
        }
        this.selectStateList = Ls;
    }

    public void init_dialog() {
        Member1 m = get_MemberManagedBean().getMember();
        this.editedCountry = countryFacade.find(m.getIDState().getIDCountry().getId());
        handleStateChange();
        editedLikings.clear();
        for (Likings l : m.getLikingsList()) {
            editedLikings.add(l.getId().toString());
        }
        System.out.println(editedLikings.size());
    }

    private MemberManagedBean get_MemberManagedBean() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        MemberManagedBean mmb = (MemberManagedBean) FacesContext.getCurrentInstance().getApplication()
                .getELResolver().getValue(elContext, null, "u_memberManagedBean");
        return mmb;
    }
}
